import { stringify } from 'qs';
import _ from 'lodash';
import request from '../utils/request';
import { Constants } from '../utils';
import { ipConfig } from './config';

const serverName = ipConfig.baseServerName();
// http://mesdev.chalco-steering.com:1708/appgateway/api-ms/pub/todo/search?appId=APP_MES&status=2&page=9&rows=10&type=1

// const PATH = '/api-ms/system/sysPubExtSysmessage/searchWithFilters';
const PATH = '/api-ms/system/sysPubMessage/searchWithFilters';
// 注册消息
const registerToPushPATH = '/api-pd/pubdata/CpsMpmApppushinfo';
// 使消息变成已读
const msgReadPATH = '/api-ms/system/sysPubExtSysmessage';

const msgReadBatchPATH = `${serverName}/device/fualt/isReadedInBatch`;

const msgCode = {
  appId: 'APP_RXSB',
};

export const search = async params => {
  // const dataPs = {
  //   sord: 'desc',
  //   sidx: 'createTime',
  //   filters: {
  //     groupOp: 'and',
  //     rules: [
  //       { field: 'appId', op: 'op', data: 'APP_RXSB' },
  //       { field: 'receiverUsercode', op: 'cn', data: params.userCode },
  //     ],
  //   },
  // };
  // const dataPs = {
  //   sorts: { isReaded: 'asc', createTime: 'desc' },
  //   filters: {
  //     groupOp: 'and',
  //     rules: [
  //       { field: 'appId', op: 'op', data: 'APP_RXSB' },
  //       { field: 'receiverUsercode', op: 'cn', data: params.userCode },
  //     ],
  //   },
  // };
  const dataPs = {
    sorts: { isReaded: 'asc', reciveTime: 'desc' },
    filters: {
      groupOp: 'and',
      rules: [
        { field: 'receiverUsercode', op: 'op', data: params.userCode },
      ],
    },
  };
  const data = JSON.stringify(dataPs);
  const defaultParams = {
    ...msgCode, rows: Constants.PAGE_SIZE, page: 1, data,
  };
  const newParams = { ...defaultParams, ...params };
  return request(`${ipConfig.url(PATH)}?${stringify(newParams)}`, {
    method: 'GET',
  }).then(mapList);
};

export const searchUpRead = async params => {
  // const dataPs = {
  //   sord: 'desc',
  //   sidx: 'createTime',
  //   filters: {
  //     groupOp: 'and',
  //     rules: [
  //       { field: 'appId', op: 'op', data: 'APP_RXSB' },
  //       { field: 'receiverUsercode', op: 'cn', data: params.userCode },
  //       { field: 'isReaded', op: 'eq', data: '0' },
  //     ],
  //   },
  // };
  const dataPs = {
    sorts: { isReaded: 'asc', reciveTime: 'desc' },
    filters: {
      groupOp: 'and',
      rules: [
        { field: 'receiverUsercode', op: 'op', data: params.userCode },
        { field: 'isReaded', op: 'op', data: '0' },
      ],
    },
  };
  const data = JSON.stringify(dataPs);
  const defaultParams = {
    ...msgCode, rows: Constants.PAGE_SIZE, page: 1, data,
  };
  const newParams = { ...defaultParams, ...params };
  return request(`${ipConfig.url(PATH)}?${stringify(newParams)}`, {
    method: 'GET',
  }).then(mapList);
};

const mapList = response => {
  const data = _.map(response.data, it => ({
    _id: it.sysmessageId,
    title: it.subject,
    content: it.content,
    time: it.reciveTime,
    isReaded: it.isReaded,
    isUnRead: it.isReaded === '0',
  }));

  return {
    list: data,
    page: response.pageNum,
    total: response.totalNum,
  };
};

export const registerToPush = async params => {
  console.info(params);
  return request(ipConfig.url(registerToPushPATH), {
    method: 'POST',
    body: params,
  }).then();
};

export const msgRead = async params => {
  console.info(params);
  return request(ipConfig.url(msgReadPATH), {
    method: 'PUT',
    body: params,
  }).then();
};

export const msgAllRead = async params => {
  console.info(params);
  return request(`${ipConfig.url(msgReadPATH)}/isReadedInBatch`, {
    method: 'PUT',
  }).then();
};

// export const msgReadBatch = async ids => {
//   return request(`${ipConfig.url(msgReadBatchPATH)}?messageIds=${ids}`, {
//     method: 'GET',
//   }).then();
// };
export const msgReadBatch = async body => {
  return request(`${ipConfig.url(msgReadBatchPATH)}`, {
    method: 'POST',
    body,
  }).then();
};
